package com.cc.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cc.common.utils.ResponseData;
import com.cc.common.utils.ResultMsg;
import com.cc.user.entity.AgreementEntity;
import com.cc.user.service.AgreementService;
import com.cc.user.vo.AgreementVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2020-12-11 11:30:58
 */
@RestController
@RequestMapping("user/agreement")
@Api(tags="协议")
public class AgreementController {

    @Autowired
    private AgreementService agreementService;
    /**
     * 修改协议
     */
    @PostMapping("/updateAgreement")
    @ApiOperation("修改协议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "协议id", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "title", value = "协议标题", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "content", value = "协议内容", paramType = "query", dataType = "String"),
    })
    public ResponseData updateAgreement(@RequestParam("id") Long id,@RequestParam("title") String title,@RequestParam("content") String content){
        AgreementEntity agreementEntity = new AgreementEntity();
        agreementEntity.setId(id);
        agreementEntity.setTitle(title);
        agreementEntity.setContent(content);
        boolean flag = agreementService.updateById(agreementEntity);
        if (flag)   return ResponseData.success("修改成功！");
        return ResponseData.success("修改失败！");
    }

    /**
     * 获取协议
     */
    @GetMapping("/getAgreement")
    @ApiOperation("获取协议")
    public ResponseData<AgreementVo> getAgreement(){
        AgreementEntity agreementEntity = agreementService.getOne(new QueryWrapper<AgreementEntity>());
        if (agreementEntity == null)   return ResponseData.success("查询失败！",true);
        AgreementVo agreementVo = new AgreementVo();
        agreementVo.setId(agreementEntity.getId());
        agreementVo.setTitle(agreementEntity.getTitle());
        agreementVo.setContent(agreementEntity.getContent());
        return ResponseData.success(agreementVo);
    }
}
